﻿$.CLogin = {
    Instance: null,
    Clear: function () {
        $("#txt_CLogin_Email,#txt_CLogin_Password").val("");
        $("#chk_CLogin_Remember").removeAttr("checked");
        $("#lbl_CLogin_Messages").text("");
    },
    Close: function () {
        var thisObj = this;
        if (thisObj.Instance)
            thisObj.Instance.Close();
    },
    Open: function (callback) {
        var thisObj = this;
        thisObj.Clear();
        thisObj.Instance = new CDialog({
            title: "• Đăng nhập •",
            closeHide: true,
            width: 550,
            height: 'auto',
            buttons: [
                {
                    text: "Đăng nhập",
                    click: function () {
                        thisObj.Login();
                    }
                }
                ,
                {
                    text: "Đóng",
                    click: function () {
                        if (typeof callback == "function")
                            callback(true);
                        thisObj.Close();
                        window.location.href = "../../Views/Home";
                    }
                }
            ]
        });
        thisObj.Instance.Init("#fsCLoginForm");
        $("#txt_CLogin_Password").unbind('keydown');
        $("#txt_CLogin_Password").bind('keydown', 'Return', function (evt) {
            thisObj.Login();
            return false;
        });
    },
    Login: function () {
        var thisObj = this;
        var pData = {
            pEmail: $("#txt_CLogin_Email").val(),
            pPassword: $("#txt_CLogin_Password").val(),
            pIsRemember: $("#chk_CLogin_Remember").is(":checked")
        };
        var overlay = new COverlay({
            autoShow: false,
            zIndex: 1100
        });
        overlay.Init();
        $.Ajax({
            url: "../Accounts/Service/AccountService.asmx/Login",
            type: 'POST',
            data: pData,
            success: function (data, textStatus, xhr) {
                if (data) {
                    data = $(data).find("string").text();
                    data = eval("(" + data + ")");
                    var IsSuccessfull = data.IsSuccessfull;
                    var Description = data.Description;
                    if (IsSuccessfull) {
                        var LastName = data.Member.LastName;
                        var Avatar = data.Member.Avatar;
                        _SYS_GLOBAL.MEMBER.ID = data.Member.ID;
                        if (location.href.indexOf("Project.aspx") != -1) {
                            Axis.View.ProfileForProject.GetMemberInfo();
                        }
                        $(".main-user-logged-Name").text(LastName);
                        $(".main-user").hide();
                        $(".main-user").hide();
                        $(".main-user-logged").show();
                        $("#imgLoggedAvatar").attr("src", Avatar);
                        thisObj.Close();
                        location.reload();
                    }
                    else {
                        $("#lbl_CLogin_Messages").text(Description);
                        overlay.Hide();
                    }
                }
                else {
                    overlay.Hide();
                }
            },
            beforeSend: function () {
                overlay.Show();
            },
            error: function (xhr, textStatus, errorThrown) {
                overlay.Hide();
            }
        });
    },
    Check: function (callback) {
        var thisObj = this;
        var overlay = new COverlay({
            autoShow: false,
            zIndex: 1100
        });
        overlay.Init();
        $.Ajax({
            url: "../Accounts/Service/AccountService.asmx/CheckLogin",
            type: 'POST',
            data: {},
            success: function (data, textStatus, xhr) {
                if (data) {
                    data = $(data).find("string").text();
                    data = eval("(" + data + ")");
                    var IsSuccessfull = data.IsSuccessfull;
                    if (!IsSuccessfull) {
                        thisObj.Open(callback);
                    }
                }
                overlay.Hide();
            },
            beforeSend: function () {
                overlay.Show();
            },
            error: function (xhr, textStatus, errorThrown) {
                overlay.Hide();
            }
        });
    }
};